//
//  LTHomeworkReportListView.m
//  LearnTeacher
//
//  Created by zyl on 2024/4/12.
//

#import "LTHomeworkReportListView.h"

@implementation LTHomeworkReportListView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.tableView];
        self.backgroundColor = LTWhiteColor;
    }
    return self;
}
#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@""];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        cell.backgroundColor = rgba(244, 245, 247, 1);
    }
    
    [cell removeAllSubviews];
    NSArray *array = self.data[indexPath.row];
    for (int i = 0; i < array.count; i++) {
        UILabel *titlelab = self.titleLabels[i];
        UILabel *contentLab = [[UILabel alloc] initWithFrame:titlelab.frame];
        contentLab.height = 28;
        contentLab.font = RFontWithSize(13);
        contentLab.text = [NSString stringWithFormat:@"%@",array[i]];
        [contentLab setTextAlignment:NSTextAlignmentCenter];
        contentLab.textColor = LTTextBlack;
        [cell addSubview:contentLab];
    }
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.data.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    return 28;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 34;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] init];
    
    headerView.frame = CGRectMake(0,0,LTScreenWidth - 64,34);
    headerView.backgroundColor = LTWhiteColor;
    for (int i = 0; i < self.titleLabels.count; i++) {
        UILabel *titlelab = self.titleLabels[i];
        [headerView addSubview:titlelab];
        if (i == 0) {
            [titlelab setCorners:UIRectCornerTopLeft cornerRadius:8];
        }
        if (i + 1 == self.titleLabels.count) {
            [titlelab setCorners:UIRectCornerTopRight cornerRadius:8];
        }
    }
    return headerView;
}
- (void)setTitles:(NSArray *)titles
{
    _titles = titles;
    
    self.titleLabels = @[].mutableCopy;
    double x = 0;
    double w = (LTScreenWidth - 64 - 4)*1.0/self.titles.count;
    for (int i = 0; i < self.titles.count; i++) {
        
        UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(x, 0, w, 32)];
        x = x + w + 1;
        titleLab.font = SFontWithSize(14);
        NSString *str = self.titles[i];
        titleLab.numberOfLines = 2;
        [titleLab setTextAlignment:NSTextAlignmentCenter];
        titleLab.textColor = LTWhiteColor;
        titleLab.backgroundColor = rgba(122, 167, 255, 1);
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:str];
        [att addAttribute:NSFontAttributeName value:SFontWithSize(14) range:str.rangeOfAll];
        if ([str containsString:@"(≥60)"]) {
            [att addAttribute:NSFontAttributeName value:RFontWithSize(8) range:[str rangeOfString:@"(≥60)"]];
        }
        if ([str containsString:@"(≥90)"]) {
            [att addAttribute:NSFontAttributeName value:RFontWithSize(8) range:[str rangeOfString:@"(≥90)"]];
        }
        titleLab.attributedText = att;
        [self.titleLabels addObject:titleLab];
    }
    [self.tableView reloadData];
    
}
- (void)setData:(NSArray *)data
{
    _data = data;
    self.tableView.height = self.data.count*28 + 34;
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    }
    [self.tableView reloadData];
}
-(UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(16, 16, LTScreenWidth - 64, 16) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        [_tableView setBackgroundColor:LTClearColor];
        [_tableView setSeparatorColor:LTClearColor];
            
    }
    
    return _tableView;
}
@end
